"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】CREATE/ALTER/DROP TRIGGER命令触发事件触发器
Create At   : 2024/07/08
Owner       : sungang14
Description :
    预置条件：
        1.创建事件触发器：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0013()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0013();
        CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0013();
        CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0013();
        CREATE EVENT TRIGGER rewrite_eventtri ON  table_rewrite EXECUTE PROCEDURE f_eventtrigger_0013(); 
       2、创建表
        CREATE TABLE t_eventtrigger_0013_1(id1 INT, id2 INT, id3 INT);
        CREATE TABLE t_eventtrigger_0013_2(id1 INT, id2 INT, id3 INT);
        3、创建函数
        CREATE OR REPLACE FUNCTION tri_insert_func() RETURNS TRIGGER AS $$ DECLARE BEGIN INSERT INTO t_eventtrigger_0013_2 VALUES(NEW.id1, NEW.id2, NEW.id3); RETURN NEW; END $$ LANGUAGE PLPGSQL;
    测试步骤：
        1、CREATE TRIGGER tri_eventtrigger_0013 BEFORE INSERT ON t_eventtrigger_0013_1 FOR EACH ROW EXECUTE PROCEDURE tri_insert_func();
        2、ALTER TRIGGER tri_eventtrigger_0013 ON t_eventtrigger_0013_1 RENAME TO new_tri_eventtrigger_0013;
        3、DROP TRIGGER new_tri_eventtrigger_0013 ON t_eventtrigger_0013_1;
Expect      : 
    1、触发执行函数f_eventtrigger_0013()
        test_event_trigger: ddl_command_start,CREATE TRIGGER
        test_event_trigger: ddl_command_end,CREATE TRIGGER
    2、触发执行函数f_eventtrigger_0013()
        test_event_trigger: ddl_command_start,ALTER TRIGGER
        test_event_trigger: ddl_command_end,ALTER TRIGGER
    3、触发执行函数f_eventtrigger_0013()
        test_event_trigger: ddl_command_start,DROP TRIGGER
        test_event_trigger: sql_drop,DROP TRIGGER
        test_event_trigger: ddl_command_end,DROP TRIGGER
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0013();")
        self.comsh.execut_db_sql('DROP TABLE if exists t_eventtrigger_0013_1;')
        self.comsh.execut_db_sql('DROP TABLE if exists t_eventtrigger_0013_2;')
        self.comsh.execut_db_sql('DROP function if exists f_eventtrigger_0013_1;')
        self.comsh.execut_db_sql('DROP TRIGGER if exists tri_eventtrigger_0013;')
        self.comsh.execut_db_sql('DROP TRIGGER if exists new_tri_eventtrigger_0013;')
        self.comsh.execut_db_sql("CREATE TABLE t_eventtrigger_0013_1(id1 INT, id2 INT, id3 INT);")
        msg = self.comsh.execut_db_sql("CREATE TABLE t_eventtrigger_0013_2(id1 INT, id2 INT, id3 INT);")
        self.logger.info(msg)
        self.assertEqual("CREATE TABLE", msg)
        msg = self.comsh.execut_db_sql(f"CREATE OR REPLACE FUNCTION f_eventtrigger_0013_1() RETURNS TRIGGER " \
                                       f"AS \$\$ DECLARE BEGIN INSERT INTO t_eventtrigger_0013_2 VALUES(NEW.id1, NEW.id2, NEW.id3); " \
                                       f"RETURN NEW; END \$\$ LANGUAGE PLPGSQL;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0013() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0013();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0013();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0013();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER rewrite_eventtri ON table_rewrite EXECUTE PROCEDURE f_eventtrigger_0013();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)

    def test_Index_file_damaged(self):
        self.logger.info(
            "创建TRIGGER:CREATE TRIGGER tri_eventtrigger_0013 BEFORE INSERT ON t_eventtrigger_0013_1 FOR EACH ROW EXECUTE PROCEDURE f_eventtrigger_0013_1();")
        msg = self.comsh.execut_db_sql(
            "CREATE TRIGGER tri_eventtrigger_0013 BEFORE INSERT ON t_eventtrigger_0013_1 FOR EACH ROW EXECUTE PROCEDURE f_eventtrigger_0013_1();")
        self.logger.info(msg)
        self.assertIn(f'CREATE TRIGGER', msg)
        self.assertIn(f'test_event_trigger: ddl_command_start CREATE TRIGGER', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end CREATE TRIGGER', msg)

        self.logger.info(
            "TRIGGER重命名:ALTER TRIGGER tri_eventtrigger_0013 ON t_eventtrigger_0013_1 RENAME TO new_tri_eventtrigger_0013;")
        msg = self.comsh.execut_db_sql(
            "ALTER TRIGGER tri_eventtrigger_0013 ON t_eventtrigger_0013_1 RENAME TO new_tri_eventtrigger_0013;")
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start ALTER TRIGGER', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end ALTER TRIGGER', msg)
        self.assertIn("ALTER TRIGGER", msg)

        self.logger.info('删除TRIGGER:DROP TRIGGER new_tri_eventtrigger_0013 ON t_eventtrigger_0013_1;')
        msg = self.comsh.execut_db_sql('DROP TRIGGER new_tri_eventtrigger_0013 ON t_eventtrigger_0013_1;')
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start DROP TRIGGER', msg)
        self.assertIn(f'test_event_trigger: sql_drop DROP TRIGGER', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end DROP TRIGGER', msg)
        self.assertIn("DROP TRIGGER", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0013();")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0013_1();")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql('DROP TABLE if exists t_eventtrigger_0013_1;')
        self.logger.info(msg)
        self.assertIn('DROP TABLE', msg)
        # 删除表
        msg = self.comsh.execut_db_sql('DROP TABLE if exists t_eventtrigger_0013_2;')
        self.logger.info(msg)
        self.assertIn('DROP TABLE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
